﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace HTToPdf
{
    public partial class Main : Form
    {
        public Main(string[] args)
        {
            InitializeComponent();
            putin = args;
        }
        YFPDFPlus yfpdf = new YFPDFPlus();
        JHPDFPlus jhpdf = new JHPDFPlus();
        XSPDFPlus xspdf = new XSPDFPlus();
        JHVToPDF jhvpdf = new JHVToPDF();
        JHTToPDF jhtpdf=new JHTToPDF();

        MsSqlHelper mssql = new MsSqlHelper();
        public string EnterpriseName = ConfigurationManager.AppSettings["EnterpriseName"].ToString();
        public string[] putin = null;
        private string sqlhz = string.Empty;
        private string sqlmx = string.Empty;

        private void Main_Load(object sender, EventArgs e)
        {
            DateTime StartDate = Convert.ToDateTime("2020-10-11");
            TimeSpan span = StartDate.Subtract(DateTime.Now);
            if (putin.Length != 0)
            {
                string djbh = putin[0].Trim();
                if (djbh.StartsWith("JHF"))
                {
                    JHToPdf(djbh);
                }
                if (djbh.StartsWith("XSG"))
                {
                    XSToPdf(djbh);
                }
                if (djbh.StartsWith("YUA"))
                {
                    YFToPdf(djbh);
                }
                if (djbh.StartsWith("JHV"))
                {
                    JHVToPdf(djbh);
                }
                if (djbh.StartsWith("JHT"))
                {
                    JHTToPdf(djbh);
                }
                this.Close();
                Application.Exit();
            }
        }
        /// <summary>
        /// 预付款SQL
        /// </summary>
        /// <param name="Number">单据编号</param>
        public void YFToPdf(string Number)
        {
            sqlmx = "select c.spmch,c.shpgg,c.dw,b.shl,b.dj,b.hsje,left(c.shengccj,45) as shengccj,rtrim(b.pihao) as pihao,rtrim(b.baozhiqi) as baozhiqi,rtrim(b.sxrq) as sxrq "
                + "from [yf_djmx] b(nolock),[spkfk] c(nolock)" 
                +" where  b.spid=c.spid and b.djbh='"+Number+"'";            
            sqlhz = "select "
                + "isnull(rtrim(a.djbh),'') as djbh,'（'+rtrim(b.danwbh)+'）'+isnull(rtrim(b.dwmch),'') as dwmch,isnull(rtrim(a.rq),'') as rq,"
                + "isnull(cast(a.hsje as varchar(20)),'') as hsje,isnull(cast(a.zkzr as varchar(20)),'') as zkzr,"
                + "isnull(rtrim(a.thfs),'') as thfs,isnull(rtrim(a.yunfcd),'') as yunfcd,"
                + "isnull(rtrim(a.fkqx),'') as fkqx,"
                //+"case when isnull(rtrim(a.jsfsmch),'')='' then isnull(rtrim(c.fkfs),'') else isnull(rtrim(a.jsfsmch),'') end as fkfs,"
                + "	(SELECT STUFF((select ','+rtrim(jsfsmch)  from yf_djmx_jsfs(nolock) where djbh =a.djbh for xml path('')),1,1,'')) as fkfs,"
                +"isnull(rtrim(c.jsms),'') as jsms,isnull(rtrim(b.faplx),'') as faplx,"
                + "isnull(rtrim(a.username),'') as username,"
                + "isnull(rtrim(dzhdh),'') as dzhdh,isnull(rtrim(khyh),'') as khyh,isnull(rtrim(yhzhh),'') as yhzhh,isnull(rtrim(shh),'') as shh,"
                + "case when isnull(rtrim(fhzq),'')='' then 3 else isnull(rtrim(fhzq),'') end as fhzq,"
                + "case when isnull(rtrim(yyzq),'')='' then 3 else isnull(rtrim(yyzq),'') end as yyzq"
                + " from [yf_djhz] a(nolock),[mchk] b(nolock) "
                + " left join [gysyfye] c(nolock) on b.dwbh=c.dwbh"
                + " where a.dwbh=b.dwbh and djbh='" + Number + "'";
            ToPDF(sqlhz, sqlmx);
        }
        /// <summary>
        /// 采购订单SQL
        /// </summary>
        /// <param name="Number">单据编号</param>
        public void JHToPdf(string Number)
        {
            sqlmx = "select c.spmch,c.shpgg,c.dw,b.shl,b.dj,b.hsje,left(c.shengccj,45) as shengccj "
                + "from [jh_ddmx] b(nolock),[spkfk] c(nolock)"
                + " where  b.spid=c.spid and b.djbh='" + Number + "'";
            sqlhz = "select "
                + "isnull(rtrim(a.djbh),'') as djbh,isnull(rtrim(a.rq),'') as rq,'（'+rtrim(b.danwbh)+'）'+isnull(rtrim(b.dwmch),'') as dwmch,"
                + "isnull(cast(a.hsje as varchar(20)),'') as hsje,'' as zkzr,"
                + "isnull(rtrim(c.jsms),'') as jsms,isnull(rtrim(c.fkfs),'') as fkfs,"
                + "isnull(rtrim(a.username),'') as username,"
                + "isnull(rtrim(dzhdh),'') as dzhdh,isnull(rtrim(khyh),'') as khyh,isnull(rtrim(yhzhh),'') as yhzhh,isnull(rtrim(shh),'') as shh,"
                + " '' as fhzq,'' as yyzq,'' as thfs,'' as yunfcd,'' as faplx"
                + " from [jh_ddhz] a(nolock),[mchk] b(nolock) "
                + " left join [gysyfye] c(nolock) on b.dwbh=c.dwbh"
                + " where a.dwbh=b.dwbh and djbh='" + Number + "'";
            ToPDF(sqlhz, sqlmx);
        }
        /// <summary>
        /// 采购退补单SQL
        /// </summary>
        /// <param name="Number">单据编号</param>
        public void JHVToPdf(string Number)
        {
            sqlmx = "select c.spmch,c.shpgg,c.dw,b.shl,b.yhshj,b.nhshj,b.hsje,left(c.shengccj,45) as shengccj,b.pihao "
                + "from [jh_djmx] b(nolock),[spkfk] c(nolock)"
                + " where  b.spid=c.spid and b.djbh='" + Number + "'";
            sqlhz = "select "
                + "isnull(rtrim(a.djbh),'') as djbh,isnull(rtrim(a.rq),'') as rq,'（'+rtrim(b.danwbh)+'）'+isnull(rtrim(b.dwmch),'') as dwmch,"
                + "isnull(cast(a.hsje as varchar(20)),'') as hsje,'' as zkzr,"
                + "isnull(rtrim(c.jsms),'') as jsms,isnull(rtrim(c.fkfs),'') as fkfs,"
                + "isnull(rtrim(a.username),'') as username,"
                + "isnull(rtrim(dzhdh),'') as dzhdh,isnull(rtrim(khyh),'') as khyh,isnull(rtrim(yhzhh),'') as yhzhh,isnull(rtrim(shh),'') as shh,"
                + " '' as fhzq,'' as yyzq,'' as thfs,'' as yunfcd,'' as faplx"
                + " from [jh_djhz] a(nolock),[mchk] b(nolock) "
                + " left join [gysyfye] c(nolock) on b.dwbh=c.dwbh"
                + " where a.dwbh=b.dwbh and djbh='" + Number + "'";
            ToPDF(sqlhz, sqlmx);
        }
        /// <summary>
        /// 采购退补单SQL
        /// </summary>
        /// <param name="Number">单据编号</param>
        public void JHTToPdf(string Number)
        {
            sqlmx = "select c.spmch,c.shpgg,c.dw,b.shl,b.hshj,b.hsje,left(c.shengccj,45) as shengccj,b.pihao "
                + "from [jh_djmx] b(nolock),[spkfk] c(nolock)"
                + " where  b.spid=c.spid and b.djbh='" + Number + "'";
            sqlhz = "select "
                + "isnull(rtrim(a.djbh),'') as djbh,isnull(rtrim(a.rq),'') as rq,'（'+rtrim(b.danwbh)+'）'+isnull(rtrim(b.dwmch),'') as dwmch,"
                + "isnull(cast(a.hsje as varchar(20)),'') as hsje,'' as zkzr,"
                + "isnull(rtrim(c.jsms),'') as jsms,isnull(rtrim(c.fkfs),'') as fkfs,"
                + "isnull(rtrim(a.username),'') as username,"
                + "isnull(rtrim(dzhdh),'') as dzhdh,isnull(rtrim(khyh),'') as khyh,isnull(rtrim(yhzhh),'') as yhzhh,isnull(rtrim(shh),'') as shh,"
                + " '' as fhzq,'' as yyzq,'' as thfs,'' as yunfcd,'' as faplx"
                + " from [jh_djhz] a(nolock),[mchk] b(nolock) "
                + " left join [gysyfye] c(nolock) on b.dwbh=c.dwbh"
                + " where a.dwbh=b.dwbh and djbh='" + Number + "'";
            ToPDF(sqlhz, sqlmx);
        }
        /// <summary>
        /// 销售订单SQL
        /// </summary>
        /// <param name="Number">单据编号</param>
        public void XSToPdf(string Number)
        {
            sqlmx = "select c.spmch,c.shpgg,c.dw,b.shl,b.dj,b.hsje,left(c.shengccj,45) as shengccj "
                + "from [pf_djmx] b(nolock),[spkfk] c(nolock)"
                + " where  b.spid=c.spid and b.djbh='" + Number + "'";
            sqlhz = "select "
                + "rtrim(isnull(a.djbh,'')) as djbh,rtrim(isnull(a.rq,'')) as rq,'（'+rtrim(b.danwbh)+'）'+isnull(rtrim(b.dwmch),'') as dwmch,"
                + "isnull(cast(a.hsje as varchar(20)),'') as hsje,'' as zkzr,"
                + "rtrim(isnull(a.thfs,'')) as thfs,rtrim(isnull(a.yunfcd,'')) as yunfcd,rtrim(isnull(c.fkfs,'')) as fkfs,"
               // + "rtrim(isnull(c.jsms,'')) as jsms,rtrim(isnull(a.faplx,'')) as faplx,"
               + "rtrim(isnull(a.xsjsfs,'')) as jsms,rtrim(isnull(a.faplx,'')) as faplx,"               
                + "rtrim(isnull(a.username,'')) as username,"
                + "+rtrim(isnull(dzhdh,'')) as dzhdh,rtrim(isnull(khyh,'')) as khyh,rtrim(isnull(yhzhh,'')) as yhzhh,rtrim(isnull(shh,'')) as shh,"
                +" '' as fhzq,'' as yyzq"
                + " from [pf_djhz] a(nolock),[mchk] b(nolock) "
                + " left join [xsdwysye] c(nolock) on b.dwbh=c.dwbh"
                + " where a.dwbh=b.dwbh and djbh='" + Number + "'";
            ToPDF(sqlhz, sqlmx);
        }

        public void ToPDF(string sqlhz,string sqlmx)
        {
            bool flag = false;

            DataTable dthz = mssql.ExecuteDt(sqlhz);
            DataTable dt = mssql.ExecuteDt(sqlmx);
            if (dthz.Rows.Count> 0)
            {
                Common.Model model = new Common.Model()
                {
                    djbh = dthz.Rows[0]["djbh"].ToString(),
                    rq = dthz.Rows[0]["rq"].ToString(),
                    dwmch = dthz.Rows[0]["dwmch"].ToString(),
                    hsje = dthz.Rows[0]["hsje"].ToString(),
                    thfs = dthz.Rows[0]["thfs"].ToString(),
                    yunfcd = dthz.Rows[0]["yunfcd"].ToString(),
                    fkfs = dthz.Rows[0]["fkfs"].ToString(),
                    jsms = dthz.Rows[0]["jsms"].ToString(),
                    faplx = dthz.Rows[0]["faplx"].ToString(),
                    username = dthz.Rows[0]["username"].ToString(),
                    dzhdh = dthz.Rows[0]["dzhdh"].ToString(),
                    khyh = dthz.Rows[0]["khyh"].ToString(),
                    yhzhh = dthz.Rows[0]["yhzhh"].ToString(),
                    shh = dthz.Rows[0]["shh"].ToString(),
                    fhzq = dthz.Rows[0]["fhzq"].ToString(),
                    yyzq = dthz.Rows[0]["yyzq"].ToString(),
                    zkzrje = dthz.Rows[0]["zkzr"].ToString()
                };


                string saveFileName = "";
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "pdf";
                saveDialog.Filter = "PDF文件|*.pdf";
                saveDialog.FileName = model.djbh;

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    saveFileName = saveDialog.FileName;

                    if (model.djbh.StartsWith("JHF"))
                    {
                        flag = jhpdf.CreatePDF(saveFileName, model, dt);
                    }
                    if (model.djbh.StartsWith("JHT"))
                    {
                        flag = jhtpdf.CreatePDF(saveFileName, model, dt);
                    }
                    if (model.djbh.StartsWith("JHV"))
                    {
                        flag = jhvpdf.CreatePDF(saveFileName, model, dt);
                    }
                    if (model.djbh.StartsWith("YUA"))
                    {
                        flag = yfpdf.CreatePDF(saveFileName, model, dt);
                    }
                    if (model.djbh.StartsWith("XSG"))
                    {
                        flag = xspdf.CreatePDF(saveFileName, model, dt);
                    }

                    if (flag)
                    {
                        FormMSG msg = new FormMSG(model.djbh);
                        msg.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("报错了！\r\nXSToPdf");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有找到单据", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            string djbh = txtdjbh.Text.Trim();
            if (djbh.StartsWith("JHF"))
            {
                JHToPdf(djbh);
            }
            if (djbh.StartsWith("XSG"))
            {
                XSToPdf(djbh);
            }
            if (djbh.StartsWith("YUA"))
            {
                YFToPdf(djbh);
            }
            if (djbh.StartsWith("JHV"))
            {
                JHVToPdf(djbh);
            }
            if (djbh.StartsWith("JHT"))
            {
                JHTToPdf(djbh);
            }
        }
    }
}
